import { Component, OnInit, Injector, ViewChild, Output, EventEmitter, Input } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';

import { finalize } from 'rxjs/operators';
import {
    StockZT_BaseInfoServiceProxy,
    Stock_BIMarketEditDto,
    EditDtoExtensionsItem
} from '@shared/service-proxies/stock-service-proxies';

@Component({
    selector: 'markbaseFieldDetailModal',
    templateUrl: './mark-basefield-detail-modal.component.html',
    styleUrls: ['./mark-basefield-detail-modal.component.less']
})
export class MarkBaseFieldDetailModalComponent extends AppComponentBase implements OnInit {
    constructor(injector: Injector, private _BaseInfoServiceProxy: StockZT_BaseInfoServiceProxy) {
        super(injector);
    }

    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    active = false;
    saving = false;
    selectedValue = new Stock_BIMarketEditDto();
    editDto;
    extensions: Array<EditDtoExtensionsItem>;
    extendextensions: Array<EditDtoExtensionsItem>;
    optionAll;
    itemcode = '';
    ngOnInit(): void {}
    showinfo(id?: string, type?: string, codeItemId?: string): void {
        this.active = true;
        this._BaseInfoServiceProxy.getMarketDtoExtensionsById(id, '').subscribe(result => {
            this.optionAll = {
                f_DimensionGroupIDOptions: result.dimentionSelectOptions,
                f_UnitIdOptions: result.unitIdSelectOptions
            };
            this.editDto = result.stock_BIMarketEditDto;
            this.extensions = result.baseItems;
            this.extendextensions = result.extendItems;

            this.extensions.forEach(el => {
                el.readonly = true;
            });
            this.extendextensions.forEach(el => {
                el.readonly = true;
            });
            this.modal.show();
        });
    }

    save(): void {
        this.saving = true;

        this._BaseInfoServiceProxy
            .createOrUpdateBIMarket(this.editDto)
            .pipe(
                finalize(() => {
                    this.saving = false;
                })
            )
            .subscribe(() => {
                this.notify.info(this.l('SavedSuccessfully'));
                this.close();
                this.modalSave.emit(null);
            });
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
